{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 集合和列表非常相似\n",
    "- 不同点：\n",
    "    1. 集合中只能存储不可变对象\n",
    "        - 如果传递了可变对象会抛出异常 `TypeError`\n",
    "    2. 集合中存储的对象是无序的（不是按照元素的插入顺序保存）\n",
    "    3. 集合中不能出现重复的元素\n",
    "    4. 无法通过索引 `index` 操作集合，否则会抛出异常 `TypeError`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建集合\n",
    "\n",
    "### 使用 `{}` 创建集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = {10,5,99,2,1,2,2,2,2}\n",
    "\n",
    "print(f's_1 = {s_1}, type(s_1) = {type(s_1)}')\n",
    "\n",
    "s_2 = {}\n",
    "print(f's_2 = {s_2}, type(s_2) = {type(s_2)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用 `set()` 创建集合\n",
    "\n",
    "- 可以通过 `set()` 来将序列和字典转换为集合\n",
    "- 使用 `set()` 将字典转换为集合时，只会包含字典中的键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4,5,5,5,5,5))\n",
    "print(f's_1 = {s_1}, type(s_1) = {type(s_1)}')\n",
    "\n",
    "s_2 = set('hello')\n",
    "print(f's_2 = {s_2}, type(s_2) = {type(s_2)}')\n",
    "\n",
    "s_3 = set(dict(a=1,b=2,c=3))\n",
    "print(f's_3 = {s_3}, type(s_3) = {type(s_3)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {1,2,3}\n",
    "\n",
    "print(f'list(s)[0] = {list(s)[0]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 `in` 和 `not in` 检查集合中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {1,2,3,'a','b','hello'}\n",
    "\n",
    "print(f\"'a' in s = {'a' in s}\")\n",
    "print(f\"'ab' in s = {'ab' in s}\")\n",
    "print(f\"'a' in s = {'a' not in s}\")\n",
    "print(f\"'ab' in s = {'ab' not in s}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 `len()` 获取集合中元素的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {1,2,3,'a','b','hello'}\n",
    "print(f'len(s) = {len(s)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 修改集合\n",
    "\n",
    "### 添加元素\n",
    "\n",
    "#### 使用 `.add()` 向集合中添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {1,2,3,'a','b','hello'}\n",
    "print(f's = {s}')\n",
    "\n",
    "s.add('world')\n",
    "print(f's = {s}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `.update()`\n",
    "\n",
    "- 将一个集合中的元素添加到当前的集合中\n",
    "- `.update()` 可以传递序列或字典作为参数，字典只会使用 `key`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set('hello')\n",
    "print(f's_1 = {s_1}')\n",
    "\n",
    "s_2 = set('world')\n",
    "print(f's_2 = {s_2}')\n",
    "\n",
    "s_1.update(s_2)\n",
    "print(f's_1 = {s_1}')\n",
    "\n",
    "s_1.update((1,2,3,4,5))\n",
    "print(f's_1 = {s_1}')\n",
    "\n",
    "s_1.update({10:'ab',20:'cd',30:'ef'})\n",
    "print(f's_1 = {s_1}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除元素\n",
    "\n",
    "#### `.pop()`\n",
    "\n",
    "- 随机删除一个集合中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4,5))\n",
    "print(f's_1 = {s_1}')\n",
    "\n",
    "rst = s_1.pop()\n",
    "print(f's_1 = {s_1}')\n",
    "print(f'rst = {rst}')\n",
    "\n",
    "rst = s_1.pop()\n",
    "print(f's_1 = {s_1}')\n",
    "print(f'rst = {rst}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `.remove()`\n",
    "\n",
    "- 删除集合中的指定元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4,5))\n",
    "print(f's_1 = {s_1}')\n",
    "\n",
    "s_1.remove(5)\n",
    "print(f's_1 = {s_1}')\n",
    "\n",
    "s_1.remove(3)\n",
    "print(f's_1 = {s_1}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `.clear()`\n",
    "\n",
    "- 清空集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4,5))\n",
    "print(f's_1 = {s_1}')\n",
    "\n",
    "s_1.clear()\n",
    "print(f's_1 = {s_1}, type(s_1) = {type(s_1)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.copy()`\n",
    "\n",
    "- 对集合进行浅复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4,5))\n",
    "s_2 = s_1.copy()\n",
    "print(f's_1 = {s_1}, id(s_1) = {id(s_1)}')\n",
    "print(f's_2 = {s_2}, id(s_2) = {id(s_2)}')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
  },
  "kernelspec": {
   "display_name": "Python 3.9.5 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}